import httplib, urllib

def translate(text, sl, tl):
    param = urllib.urlencode({
        "v":"1.0",
        "q":text,
        "langpair":"%s|%s"%(sl,tl)
    })

    conn = httplib.HTTPConnection("ajax.googleapis.com")
    conn.request("GET","/ajax/services/language/translate?"+param,None,{ "Referer":"http://www.kodfabrik.com" })
    return eval(conn.getresponse().read().decode("utf8").replace("\u0026","").replace("#39;","'").replace("null","None"))["responseData"]["translatedText"]

lang = { 'bg':'Bulgarian', 'ca':'Catalan', 'zh-CN':'Chinese', 'hr':'Croatian', 'cs':'Czech', 'da':'Danish', 'nl':'Dutch', 'tl':'Filipino', 'fi':'Finnish', 'fr':'French', 'de':'German', 'el':'Greek', 'iw':'Hebrew', 'hi':'Hindi', 'id':'Indonesian', 'it':'Italian', 'ja':'Japanese', 'ko':'Korean', 'lv':'Latvian', 'lt':'Lithuanian', 'no':'Norwegian', 'pl':'Polish', 'pt':'Portuguese', 'ro':'Romanian', 'ru':'Russian', 'sr':'Serbian', 'sk':'Slovak', 'sl':'Slovenian', 'es':'Spanish', 'sv':'Swedish', 'uk':'Ukrainian', 'vi':'Vietnamese' }

source = open("../static/lang/en.js").read()
text = eval(source.replace("main.text = ",""))

for lkey in lang:
  print "New translate: %s --------------------------------"%lkey
  f = open("../static/lang/%s.js"%lkey,"w")
  f.write("main.text = {")
  for tkey in text:
    print "New sentence: %s"%tkey
    
    f.write( "\n'%s':"%tkey )
    
    
    
    if tkey=='pn':
      f.write('[')
      for i in range(len(text[tkey])):
        pname = text[tkey][i]
        print "pname: "+pname
        f.write(i>0 and "," or "")
        f.write("\"%s\""%translate(pname,'en',lkey))
      f.write('],')
    else:
      translation = translate(text[tkey],'en',lkey)
      
      if tkey=="name":
        tf = open("../title","a")
        tf.write(" '%s': '%s', "%(lkey,translation))
        tf.close()
      
      f.write( "\"%s\","%(translation) )
  f.write("\n}")
